#include "widget.h"
#include "ui_widget.h"

#include <QDebug>
#include <QtMath>

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);

    //UiPropertySetup();

    connect(ui->spinBoxNum, SIGNAL(valueChanged(int)), this, SLOT(on_pushButtonCalc_clicked()));
    connect(ui->doubleSpinBoxPrice, SIGNAL(valueChanged(double)), this, SLOT(on_pushButtonCalc_clicked()));
}

Widget::~Widget()
{
    if (ui) {
        delete ui;
        ui = nullptr;
    }
}

void Widget::UiPropertySetup()
{
    ui->doubleSpinBoxPrice->setDecimals(2);
    ui->doubleSpinBoxTotal->setDecimals(2);

    ui->spinBoxDec->setStepType(QAbstractSpinBox::AdaptiveDecimalStepType);
    ui->spinBoxDec->setRange(0, 65535);

    ui->spinBoxBin->setRange(0, 65535);
    ui->spinBoxBin->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
    ui->spinBoxBin->setDisplayIntegerBase(2);

    ui->spinBoxHex->setRange(0, 65535);
    ui->spinBoxHex->setDisplayIntegerBase(16);
}

void Widget::on_pushButtonCalc_clicked()
{
    int num = ui->spinBoxNum->value();
    double price = ui->doubleSpinBoxPrice->value();
    double total = num * price;
    ui->doubleSpinBoxTotal->setValue(total);
}

void Widget::on_spinBoxDec_valueChanged(int val)
{
    qDebug("%s : %s", "Dec", ui->spinBoxDec->cleanText().toLocal8Bit().constData());
    ui->spinBoxBin->setValue(val);
    ui->spinBoxHex->setValue(val);
}

void Widget::on_spinBoxBin_valueChanged(int val)
{
    qDebug("%s : %s", "Bin", ui->spinBoxBin->cleanText().toLocal8Bit().constData());
    ui->spinBoxDec->setValue(val);
    ui->spinBoxHex->setValue(val);
}

void Widget::on_spinBoxHex_valueChanged(int val)
{
    qDebug("%s : %s", "Hex", ui->spinBoxHex->cleanText().toLocal8Bit().constData());
    ui->spinBoxDec->setValue(val);
    ui->spinBoxBin->setValue(val);
}

